package com.example.demozy.controller;

import com.example.demozy.api.CommonResult;
import com.example.demozy.entity.RealNameUser;
import com.example.demozy.entity.User;
import com.example.demozy.entity.UserBasicInformation;
import com.example.demozy.service.UserService;
import com.example.demozy.util.IdCardUtil;
import com.example.demozy.util.IdCardVerificationUtil;
import com.example.demozy.vo.BasicInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;

/**
 * @author 王成龙
 */
@RestController
public class UserController {
    // **************** 公开变量

    // **************** 私有变量

    @Autowired
    private UserService userService;

    // **************** 继承方法

    // **************** 公开方法

    /**
     * 得到用户基本信息
     * @param userId
     * @return
     */
    @PostMapping(value = "/getBasicUserInfo")
    public CommonResult getBasicUserInfo(@RequestParam("userId") int userId) throws ParseException{
        UserBasicInformation basicInformation = userService.getUserInfoById(userId);
        User user = userService.getUserByUserId(userId);
        if (basicInformation == null) {
            return CommonResult.failed("未获取到用户信息");
        }
        BasicInfoVO basicInfoVO = new BasicInfoVO();

        if (user.getRealStat() == 0) {
            basicInfoVO.setRealName("未实名");
            basicInfoVO.setIdCardNumber("未实名");
        } else if (user.getRealStat() == 1) {
            RealNameUser realNameUser = userService.getRealNameUserById(userId);
            String idCardNumber = realNameUser.getIdCardNumber();
            basicInfoVO.setRealName(realNameUser.getRealName());
            basicInfoVO.setIdCardNumber(realNameUser.getIdCardNumber());
            //根据身份证号获取性别、生日
            basicInfoVO.setRealStat(user.getRealStat());
            basicInfoVO.setGender(IdCardUtil.getSex(idCardNumber));
            basicInfoVO.setBirthday(IdCardUtil.getBirthday(idCardNumber));
        }

        basicInfoVO.setUserId(user.getUserId());
        basicInfoVO.setAvatar(basicInformation.getAvatar());
        basicInfoVO.setNickname(basicInformation.getNickname());
        basicInfoVO.setMobile(user.getMobile());
        basicInfoVO.setEmail(user.getEmail());
        basicInfoVO.setUserDescription(basicInformation.getUserDescription());

        return CommonResult.success(basicInfoVO, "获取基本信息成功");
    }

    /**
     * 用户修改基本资料
     * @param basicInfoVO
     * @return
     */
    @PostMapping(value = "/updateBasicInfo")
    public CommonResult updateBasicInfo(@RequestBody BasicInfoVO basicInfoVO) {
        int userId = basicInfoVO.getUserId();
        String nickname = basicInfoVO.getNickname();
        String email = basicInfoVO.getEmail();
        String userDescription = basicInfoVO.getUserDescription();

        userService.updateBasicInfo(userId, nickname, userDescription);
        userService.updateEmailById(userId, email);
        return CommonResult.success("修改成功");
    }

    /**
     * 实名认证
     * @param realNameUser
     * @return
     * @throws ParseException
     */
    @PostMapping(value = "/realNameAuthentication")
    public CommonResult realNameAuthentication(@RequestBody RealNameUser realNameUser) throws
            ParseException {
        int userId = realNameUser.getUserId();
        String realName = realNameUser.getRealName();
        String idCardNumber = realNameUser.getIdCardNumber();

        if (("").equals(realName) || ("").equals(idCardNumber)) {
            return CommonResult.failed("姓名或身份证号不能为空");
        }
        //身份证号格式认证
        if (IdCardVerificationUtil.IDCardValidate(idCardNumber).getCode() == 200L) {
            if (userService.listRealNameUsersByIdNo(idCardNumber).size() == 0) {
                //实名认证
                userService.realNameAuthentication(userId, realName, idCardNumber);
                //根据身份证号获取性别与出生日期并在基本信息表更新
                String gender = IdCardUtil.getSex(idCardNumber);
                Date birthday = IdCardUtil.getBirthday(idCardNumber);
                userService.updateGenderAndBirthday(userId, gender, birthday);
                return CommonResult.success("实名认证成功");
            } else {
                return CommonResult.failed("此信息已被认证，请重新输入");
            }
        } else {
            return IdCardVerificationUtil.IDCardValidate(idCardNumber);
        }
    }

    @PostMapping(value = "/getRealStat")
    public CommonResult getRealStat(@RequestParam("userId") int userId) {
        int realStat = userService.getRealStatById(userId);
        return CommonResult.success(realStat);
    }

    // **************** 私有方法

}
